package com.canaan.business.controller.pc.invoice;

import com.canaan.business.domain.invoice.WmsSalesOrder;
import com.canaan.business.domain.vo.WmsSalesOrderVo;
import com.canaan.business.service.invoice.IWmsSalesOrderService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 销售订单Controller
 *
 * @author ruoyi
 * @date 2022-12-13
 */
@Api(tags = "销售订单管理")
@RestController
@RequestMapping("/invoice/salesOrder")
public class WmsSalesOrderController extends BaseController
{
    @Resource
    private IWmsSalesOrderService wmsSalesOrderService;

    /**
     * 查询销售订单列表
     */
    @ApiOperation(value = "查询销售订单列表")
    @GetMapping("/list")
    public TableDataInfo list(WmsSalesOrder wmsSalesOrder)
    {
        startPage();
        List<WmsSalesOrder> list = wmsSalesOrderService.selectWmsSalesOrderList(wmsSalesOrder);
        return getDataTable(list);
    }

    /**
     * 销售订单导入模板
     */
    @ApiOperation(value = "销售订单导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<WmsSalesOrderVo> util = new ExcelUtil<WmsSalesOrderVo>(WmsSalesOrderVo.class);
        util.importTemplateExcel(response, "销售订单数据");
    }

    /**
     * 销售订单列表
     */
    @ApiOperation(value = "导入销售订单列表")
    @PostMapping("/importExcel")
    @Log(title = "销售订单", businessType = BusinessType.IMPORT)
    public AjaxResult importExcel(MultipartFile file)
    {
        List<WmsSalesOrderVo> list;
        try {
            //读取excel文件数据
            ExcelUtil<WmsSalesOrderVo> util = new ExcelUtil<WmsSalesOrderVo>(WmsSalesOrderVo.class);
            list = util.importExcel(file.getInputStream());
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("导入文件解析失败");
        }
        //初步校验数据
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException("文件缺少数据");
        }

        //深入校验数据和保存数据
        List<String> errorList = wmsSalesOrderService.importExcel(list);
        if(!errorList.isEmpty()){
            StringBuilder failureMsg = new StringBuilder();
            failureMsg.append("很抱歉，导入失败！共 ").append(errorList.size()).append(" 条数据格式不正确，错误如下：");
            for (int i = 0; i < errorList.size(); i++) {
                failureMsg.append("<br/>").append(i + 1).append("、").append(errorList.get(i));
            }
            return error(failureMsg.toString());
        }
        return success("导入成功");
    }

    /**
     * 导出销售订单列表
     */
    @ApiOperation(value = "导出销售订单列表")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsSalesOrder wmsSalesOrder)
    {
        List<WmsSalesOrderVo> list = wmsSalesOrderService.exportList(wmsSalesOrder);
        ExcelUtil<WmsSalesOrderVo> util = new ExcelUtil<WmsSalesOrderVo>(WmsSalesOrderVo.class);
        util.exportExcel(response, list, "销售订单数据");
    }

    /**
     * 获取销售订单详细信息
     */
    @ApiOperation(value = "获取销售订单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsSalesOrderService.selectWmsSalesOrderById(id));
    }

    /**
     * 新增销售订单
     */
    @ApiOperation(value = "新增销售订单")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsSalesOrder wmsSalesOrder)
    {
        return toAjax(wmsSalesOrderService.insertWmsSalesOrder(wmsSalesOrder));
    }

    /**
     * 修改销售订单
     */
    @ApiOperation(value = "修改销售订单")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsSalesOrder wmsSalesOrder)
    {
        return toAjax(wmsSalesOrderService.updateWmsSalesOrder(wmsSalesOrder));
    }

    /**
     * 删除销售订单
     */
    @ApiOperation(value = "删除销售订单")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsSalesOrderService.deleteWmsSalesOrderByIds(ids));
    }

    /**
     * 销售订单发货
     */
    @ApiOperation(value = "销售订单发货")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping("/shipments")
    public AjaxResult shipments(@RequestBody List<Long> ids)
    {
        if(ids == null || ids.isEmpty()){
            throw new ServiceException("缺少必要参数");
        }

        return toAjax(wmsSalesOrderService.shipments(ids));
    }


    /**
     * 销售订单确认
     */
    @ApiOperation(value = "销售订单确认")
    @Log(title = "销售订单确认", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException("缺少必要参数");
        }
        return toAjax(wmsSalesOrderService.confirm(ids));
    }


    /**
     * 订单取消
     */
    @ApiOperation(value = "订单取消")
    @Log(title = "订单取消", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody List<Long> ids) {
        return success(wmsSalesOrderService.cancel(ids));
    }


    /**
     * 订单确认取消
     */
    @ApiOperation(value = "订单确认取消")
    @Log(title = "订单取消", businessType = BusinessType.UPDATE)
    @PutMapping("/confirmCancel")
    public AjaxResult cancel(@RequestBody WmsSalesOrder wmsSalesOrder) {
        if(wmsSalesOrder == null || wmsSalesOrder.getId() == null){
            throw new ServiceException("缺少必要参数");
        }
        return success(wmsSalesOrderService.confirmCancel(wmsSalesOrder));
    }
}
